import store from '../store'
// const files = require.context('.', false, /\.js$/)

// 字段转换成框架需要的字段
export function toMenu (sub) {
  let subList = []
  let btnList = []
  let routeList = []

  console.log(sub, 'sub')
  // 0 TITLE, 1 MENU, 2 BUTTON
  if (sub.length > 0) {
    const toMenuFun = (subListes, sub) => {
      sub.forEach((item) => {
        if (item.type === 0 || item.type === 1) {
          let obj = {}
          obj.key = item.key
          item.icon && (obj.type = item.icon)
          obj.label = item.title
          item.path && (obj.to = '/' + item.path) 
          // && routeList.push(item.path)
          obj.children = []
          if (item.children && item.children.length) {
            toMenuFun(obj.children, item.children)
          }
          // if (!store.state.user.userInfo.merchantRespParams) {
          //   obj.key !== 'center' && subListes.push(obj)
          // } else {
            routeList.push(obj)
            if(!!item.isHidden){
              subListes.push(obj)
            }
          // }
        } else if (item.type === 2) {
          let arr = item.permission
          btnList.push(arr)
        }
      })
    }
    toMenuFun(subList, sub)
    // subList = subList.filter(item => item.key !== 'personalTitle')
    // routeList = subList.filter(item => !!item.isHidden )
    let list = { subList: subList, btnList: btnList, permissionRouter: routeList }
    // let list = { subList: subList, btnList: btnList, permissionRouter: routeList }
    console.log(list)
    return list
  } else {
    return { subList: [], btnList: [], permissionRouter: [] }
  }
}

// 转成所需路由格式
export function toRouter (arr, parentName) {
  let att = []
  arr.forEach(item => {
    // let name =''
    // let routerName = ''
    let obj = []
    if(!!item.children&&item.children.length>0){
      obj = toRouter(item.children, item.label)
    } else {
      let str = []
      str[0] = '@/views/'+item.key
      // const str = require.context('../views/'+item.key, true, /\.vue$/);
      // const str = require.context('./', true, /\.js$/)
      // console.log(str, 'str45454545')
      // debugger
      obj[0] = {
        path: '/'+item.key,
        name: parentName+'/'+item.label,
        component: resolve =>
          require ([`@/views/${item.key}`], resolve),
          meta: {
            keepAlive: item.keepAlive
          }
      }
    }
    att = [...att, ...obj]
    // console.log(obj[0], att, 999999999999)
  })
  console.log(att, 'console')
  return att
}
